package com.usian.admin.controller;

import com.alibaba.fastjson.JSON;
import com.usian.admin.fegin.SearchFegin;
import com.usian.common.trie.Trie;
import com.usian.model.article.UserSearchDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.search.ApAssociateWords;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/ApArticle")
public class ApArticleSearchController {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private SearchFegin searchFegin;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("findByTitle")
    public SearchResponse findByTitle(@RequestBody UserSearchDto userSearchDto) throws IOException {
        SearchRequest searchRequest = new SearchRequest("app_info_article");




        searchRequest.source().sort("publishTime", SortOrder.DESC);

        searchRequest.source().query(QueryBuilders.matchQuery("title",userSearchDto.getSearchWords())) .size(5);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        ApUser user = AppThreadLocalUtils.getUser();

        searchFegin.user_SearchAdd(user.getId(),userSearchDto.getSearchWords());


        return search;
    }

    @RequestMapping("findByAssociate")
    public ResponseResult findByAssociate(@RequestBody UserSearchDto userSearchDto) throws IOException {
        String associate_list = (String) redisTemplate.opsForValue().get("associate_list");
        List<ApAssociateWords> apAssociateWords=null;

        if(StringUtils.isNotEmpty(associate_list)){
            apAssociateWords = JSON.parseArray(associate_list, ApAssociateWords.class);
        }else{
            apAssociateWords = searchFegin.findAllAssociate();
            redisTemplate.opsForValue().set("associate_list",JSON.toJSONString(apAssociateWords));
        }

        Trie trie = new Trie();
        for (ApAssociateWords apAssociateWord : apAssociateWords) {
            trie.insert(apAssociateWord.getAssociateWords());
        }

        List<String> strings = trie.startWith(userSearchDto.getSearchWords());
        List<ApAssociateWords> objects = new ArrayList<>();
        for (String string : strings) {
            ApAssociateWords apAssociateWords1 = new ApAssociateWords();
            apAssociateWords1.setAssociateWords(string);
            objects.add(apAssociateWords1);
        }
        return ResponseResult.okResult(objects);
    }


}